package io.gitee.tuo_tuo.jfinaldevassistance.controller;

import com.jfinal.core.Controller;

/**
 * 描述:
 * 版权: Copyright (c) 2018
 * 公司:
 * 作者: tuotuo
 * 版本: 1.0
 * 创建日期: 2018/5/8
 * 创建时间: 16:52
 */
public abstract class ControllerSupport extends Controller{

//	protected User user;
	protected int pageNumber;
	protected int pageSize;

	public final void init(){
		initPagedAndUserInfo();
		initController();
	}

	public void initPagedAndUserInfo(){
		if(getRequest() != null){
//			user = getSessionAttr(UserConstants.SESSION_USER);
			pageNumber = getParaToInt("pageNumber", 1);
			pageSize = getParaToInt("pageSize", 10);
		}
	}

	/**
	 *	被实际控制器override，初始化实际控制器
	 */
	public void initController(){

	}

	/**
	 * Get models from http request.
	 */
//	public <T> List<T> getModels(Class<T> modelClass) {

//		List modelList = new ArrayList<>();
////		Map<Integer, Model> modelMap = new HashMap<>();
////		try {
////			T instance = modelClass.newInstance();
////			if(instance instanceof Model == false){
////				throw new IllegalArgumentException("getModels only support class of Model, using getBean for other class.");
////			}
////			Model db = (Model)instance;
////
////			Table table = TableMapping.me().getTable(db.getClass());
////			if (table == null) {
////				throw new ActiveRecordException("The Table mapping of db: " + modelClass.getName() +
////						" not exists or the ActiveRecordPlugin not start.");
////			}
////
////			Map<String, String[]> parasMap = getRequest().getParameterMap();
////			int index;
////			Pattern pattern = Pattern.compile("\\[(\\d+)\\]\\[(.*)\\]");
////			for (Map.Entry<String, String[]> entry : parasMap.entrySet()) {
////				String paraName = entry.getKey();
////				String attrName;
////
////				Matcher matcher = pattern.matcher(paraName);
////				if (matcher.find() == false) {
////					continue;
////				}
////				index = Integer.parseInt(matcher.group(1));
////				attrName = matcher.group(2);
////
////				db = modelMap.get(index);
////				if(db == null){
////					db = (Model) modelClass.newInstance();
////				}
////
////				Class<?> colType = table.getColumnType(attrName);
////				if (colType == null) {
////					continue;
////				}
////
////				String[] paraValueArray = entry.getValue();
////				String paraValue = (paraValueArray != null && paraValueArray.length > 0) ? paraValueArray[0] : null;
////
////				Object value = paraValue != null ? TypeConverter.convertObject(colType, paraValue) : null;
////				db.set(attrName, value);
////				modelMap.put(index, db);
////			}
////
////		} catch (Exception e) {
////			throw new RuntimeException(e);
////		}
////		Collection<Model> values = modelMap.values();
////		Iterator<Model> iterator = values.iterator();
////		while (iterator.hasNext()){
////			modelList.add(iterator.next());
////		}
////		return modelList;
//	}

}
